import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.jiangdg.ausbc.utils.ToastUtils
import com.jiangdg.demo.R
import razerdp.basepopup.BasePopupWindow
import java.io.File

class CreatePatientPopup(context: Context, private val folderName: String? = null,private val onCreateSuccessListener: OnCreateSuccessListener) : BasePopupWindow(context) {

    private val patientNameInput: EditText
    private val maleOption: TextView
    private val femaleOption: TextView
    private val createButton: TextView


    private var selectedGender: String? = null

    init {
        val contentView = LayoutInflater.from(context).inflate(R.layout.popup_create_patient, null)
        setContentView(contentView)

        patientNameInput = contentView.findViewById(R.id.patient_name_input)
        maleOption = contentView.findViewById(R.id.male_option)
        femaleOption = contentView.findViewById(R.id.female_option)
        createButton = contentView.findViewById(R.id.create_button)
        contentView.findViewById<ImageView>(R.id.ivClose).setOnClickListener { dismiss() }

        maleOption.setOnClickListener {
            selectedGender = "男"
            maleOption.isSelected = true
            femaleOption.isSelected = false
        }

        femaleOption.setOnClickListener {
            selectedGender = "女"
            maleOption.isSelected = false
            femaleOption.isSelected = true
        }

        createButton.setOnClickListener {
            val patientName = patientNameInput.text.toString().trim()
            if (patientName.isNotEmpty() && selectedGender != null) {
                if (folderName == null) {
                    createPatientFolder(patientName, selectedGender!!)
                } else {
                    updatePatientFolder(folderName, patientName, selectedGender!!)
                }

            }
        }

        folderName?.let {
            displayFolderData(it)
            createButton.text = "保存"
        }

        // Set the size of the popup
        setWidth(dpToPx(context, 480))
        setHeight(dpToPx(context, 300))

        popupGravity = Gravity.CENTER
    }

    private fun createPatientFolder(patientName: String, gender: String) {
        val folderName = "$patientName~~~$gender"
        val usersDir = File(context.filesDir, "users")
        if (!usersDir.exists()) {
            usersDir.mkdirs()
        }
        val newPatientDir = File(usersDir, folderName)
        if (!newPatientDir.exists()) {
            newPatientDir.mkdir()
            ToastUtils.show("创建成功")
            onCreateSuccessListener.onSuccess(folderName)
            dismiss()
        }else{
            ToastUtils.show("患者已存在")
        }
    }

    private fun updatePatientFolder(oldFolderName: String, patientName: String, gender: String) {
        val usersDir = File(context.filesDir, "users")
        val oldFolder = File(usersDir, oldFolderName)
        if (oldFolder.exists()) {
            val newFolderName = "$patientName~~~$gender"
            val newFolder = File(usersDir, newFolderName)
            if (!newFolder.exists()) {
                oldFolder.renameTo(newFolder)
                ToastUtils.show("编辑成功")
                onCreateSuccessListener.onEditSuccess(newFolder.name)
                dismiss()
            } else {
                // 如果新文件夹已经存在，处理冲突逻辑
                // 可以在此添加提示用户文件夹已存在的逻辑
                ToastUtils.show("患者已存在")
            }
        }

    }

    private fun displayFolderData(folderName: String) {
        val parts = folderName.split("~~~")
        if (parts.size == 2) {
            patientNameInput.setText(parts[0])
            selectedGender = parts[1]
            if (selectedGender == "男") {
                maleOption.isSelected = true
            } else if (selectedGender == "女") {
                femaleOption.isSelected = true
            }
        }
    }

    private fun dpToPx(context: Context, dp: Int): Int {
        return (dp * context.resources.displayMetrics.density).toInt()
    }


    interface OnCreateSuccessListener{
        fun onSuccess(name:String)
        fun onEditSuccess(name:String)
    }
}
